<!doctype html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/modules/cms/front/include/taglib.jsp"%>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>山东林业扶贫项目</title>
  <meta name="description" content="山东林业扶贫项目">
  <meta name="keywords" content="index">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="icon" type="image/png" href="${ctxStatic}/sdlyfp/i/favicon.png">
  <link rel="apple-touch-icon-precomposed" href="${ctxStatic}/sdlyfp/i/app-icon72x72@2x.png">
  <meta name="apple-mobile-web-app-title" content="Amaze UI" />
  <link rel="stylesheet" href="${ctxStatic}/sdlyfp/css/amazeui.min.css" />
  <link rel="stylesheet" href="${ctxStatic}/sdlyfp/css/admin.css">
  <link rel="stylesheet" href="${ctxStatic}/sdlyfp/css/app.css">
  <script src="${ctxStatic}/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
  <link rel="stylesheet" href="${ctxStatic}/sweetalert/sweetalert.css">
  <script src="${ctxStatic}/sweetalert/sweetalert.min.js"></script>
  <script src="${ctxStatic}/sdlyfp/js/base.js"></script>
  
</head>

<body data-type="generalComponents">

	<!-- head -->
	<jsp:include page="head.jsp" flush="false"></jsp:include>
	
	<div class="tpl-page-container tpl-page-header-fixed">
	
	<!-- 菜单 -->
	<jsp:include page="menu.jsp" flush="false"></jsp:include>


        <div class="tpl-content-wrapper" >
            <div class="tpl-content-page-title">
                数据导出
            </div>
            <ol class="am-breadcrumb">
                <li><a href="#" class="am-icon-home">首页</a></li> 
                <li class="am-active">数据导出</li>
            </ol>
            <div class="tpl-portlet-components" style="padding-bottom: 300px;">
                <div class="portlet-title">
                   <!--  <div class="caption font-green bold">
                        <span class="am-icon-code"></span> 数据导出
                    </div> -->
                    <div >  
                    	<a href="#" onclick="toExport();"><button type="button" id="start" class="am-btn am-btn-default am-btn-success"><span></span> 开始导出</button></a>
                    	&nbsp;&nbsp;
                    	请选择备份文件(下载后的文件将被删除)
                    	<select id="dmpSel" name="dmpName">
                    		<option value="">---请选择备份文件---</option>
                    		<c:forEach items="${dmpFiles }" var="dmp">
                    			<option value="${dmp }">${dmp }</option>
                    		</c:forEach>
                    	</select>
                    	
                    	<a onclick="toDownLoad();"><button type="button" id="downLoad" class="am-btn am-btn-default am-btn-success"><span></span> 数据文件下载</button></a>
                    	
                    </div>

                </div>
                <div class="tpl-block" >
                	<div style="margin-left: 100px;"> 
						<font color="red" style="font-weight: bold;display: none;" id="tip"></font>
					</div>
				    
					<div style="margin-left: 100px;" id="logDiv"> 
					</div>
                </div>
                <div class="tpl-alert"></div>
            </div>

        </div>
    </div>
    <iframe id="downFrame" onreadystatechange="init();" onchange="init();" onloadstart="init();" src="" style="display: none" >
    </iframe>
  <script src="${ctxStatic}/sdlyfp/js/jquery.min.js"></script>
  <script src="${ctxStatic}/sdlyfp/js/amazeui.min.js"></script>
  <script src="${ctxStatic}/sdlyfp/js/app.js"></script>
</body>
<script type="text/javascript">

var startLine = 0;
var interv;

$(document).ready(function() {
	//$(".tpl-left-nav-menu").find("li a").eq(2).addClass("active");
	var menuId = '${menuId }';
	if(menuId)
	{
		$(".tpl-left-nav-menu").find("#"+menuId).addClass("active");
	}
	
	var message = "${message}";
	if(message)
	{
		var messageType = "${messageType}";
		msg(message,3000,messageType);
	}
});

function exportLog()
{
	var clientData = {"startLine":startLine};
	$.ajax({
		url:"${ctx}/gt/exportDB/exportLog",
		type:"post",
		async:true,
		data:clientData,
		success:function(data){
			data = eval('(' + data + ')');
			if(data.success){
				startLine = data.startLine;
				var logDiv = $("#logDiv").html();
				var msg = data.data;
				/* if(logDiv)
				{ */
					$("#logDiv").html(logDiv + msg)
				//}
			}else{
				
            }
		}
	});
}

function readLog()
{
	interv = window.setInterval("exportLog()",5000); 
}

function toExport()
{
	comfirmMsg("导出数据需要一定的时间，需要避开系统使用高峰期，确认要导出数据吗？",startExport,"warning");
}

function startExport()
{
	// 导出运行时，开始位置清零
	startLine = 0;
	
	$("#logDiv").html("");
	$("#tip").show();
	$("#tip").html("<img style='margin-left: 100px;' src='${ctxStatic}/sdlyfp/img/loading-2.gif'/>正在导出数据，请稍后...");
	$("#start").attr("disabled",true);
	readLog();
	var clientData = {};
	$.ajax({
		url:"${ctx}/gt/exportDB/startExport",
		type:"post",
		async:true,
		timeout : 300000,
		data:clientData,
		success:function(data){
			clearInterval(interv);
			data = eval('(' + data + ')');
			$("#tip").show();
			if(data.success){
				var msg = data.massage;
				$("#tip").html(msg);
			}else{
				$("#tip").html(msg);
            }
			$("#start").attr("disabled",false);
			
			// 导出运行完毕后，直接获取日志
			exportLog();
		}
	});
}

function toDownLoad()
{
	var dmpName = $("#dmpSel").val();
	if(!dmpName)
	{
		msg("请选择备份文件",3000,"warning");
		return;
	}
	
	comfirmMsg("下载数据文件需要一定的时间，需要避开系统使用高峰期，下载后请务必保存好文件，确认要下载数据文件" + dmpName + "吗？",startDownLoad,"warning");
}

function startDownLoad()
{
	var dmpName = $("#dmpSel").val();
	if(!dmpName)
	{
		msg("请选择备份文件",3000,"warning");
		return;
	}
	
	$("#logDiv").html("");
	$("#tip").show();
	$("#tip").html("<img style='margin-left: 100px;' src='${ctxStatic}/sdlyfp/img/loading-2.gif'/>正在下载数据，请稍后...");
	
	var timeStamp = new Date().getTime();
	$("#downLoad").attr("disabled",true);
	$("#downFrame")[0].src="${ctx}/gt/exportDB/downLoadDmp?dmpName=" + dmpName + "&timeStamp=" + timeStamp;	
	
	var interVal = setInterval(function(){
		var downToken = getCookie('downToken');
		var fileSize = getCookie('fileSize');
		if(downToken == timeStamp)
		{
			clearInterval(interVal);
			// $("#tip").hide();
			if('null' == fileSize)
			{
				$("#tip").html("没有找到文件。");
			}
			else
			{
				$("#tip").html("导出数据成功。");
			}
			
			$("#downLoad").attr("disabled",false);
		}
	},200);
}

</script>
</html>